package invest.data.getdata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.XueqiuCookie;
import invest.pojo.datapojo.KlineHistory;
import invest.utils.HttpRequest;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class HKUSStockDayKLineData implements Data<List<KlineHistory>> {
    private String code="";
    //    private String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SZ128033&begin=1617021934480&period=day&type=before&count=-100000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";
    private String pre = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=";
    private String p1 = "&begin=";
    private String after = "&period=day&type=before&count=-10&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";

    public HKUSStockDayKLineData(String code) {
        this.code = code;
    }

    @Override
    public List<KlineHistory> getData() throws IOException {
        String cookie = XueqiuCookie.getXueqiuCookie();
        Long timeMillis = System.currentTimeMillis(); // 当前时间戳
        String url = pre + code + p1 + String.valueOf(timeMillis) + after;
        String result = HttpRequest.sendGet(url,  cookie);
        JSONObject jb = JSONObject.parseObject(result);
        JSONObject data = jb.getJSONObject("data");
        JSONArray item = data.getJSONArray("item");
        List<KlineHistory> hkStockDayKlines = new ArrayList<>();
        if (item == null) {
            return null;
        }
        for (int i = 0; i <item.size() ; i++) {
            JSONArray jsonArray=item.getJSONArray(i);
            KlineHistory klineHistory = new KlineHistory();
            klineHistory.setDate(new Date((Long) jsonArray.get(0)));
            klineHistory.setVolume((Integer) jsonArray.get(1));
            klineHistory.setOpen(new Double(((BigDecimal) jsonArray.get(2)).doubleValue()));
            klineHistory.setHigh(new Double(((BigDecimal) jsonArray.get(3)).doubleValue()));
            klineHistory.setLow(new Double(((BigDecimal) jsonArray.get(4)).doubleValue()));
            klineHistory.setClose(new Double(((BigDecimal) jsonArray.get(5)).doubleValue()));
//            klineHistory.setChg(new Double(((BigDecimal) jsonArray.get(6)).doubleValue()));
//            klineHistory.setPercent(new Double(((BigDecimal) jsonArray.get(7)).doubleValue()));
            if (jsonArray.get(8)== null) {
//                klineHistory.setTurnoverrate(new Double(((BigDecimal) jsonArray.get(8)).doubleValue()));
                break;
            } else {
              klineHistory.setTurnoverrate(new Double(((BigDecimal) jsonArray.get(8)).doubleValue()));
            }

//            klineHistory.setAmount(new Double(((BigDecimal) jsonArray.get(9)).doubleValue()));
            hkStockDayKlines.add(klineHistory);
        }
        Collections.reverse(hkStockDayKlines);
        return hkStockDayKlines;
    }

    public static void main(String[] args) throws IOException {
        HKUSStockDayKLineData HKUSStockDayKLineData = new HKUSStockDayKLineData("SZ128033");
        List<KlineHistory> klineHistories = HKUSStockDayKLineData.getData();
        System.out.println(1);
    }
}
